package com.wangmeng.book.updater;

import com.wangmeng.book.dao.BaseDaoSupport;
import com.wangmeng.book.entity.*;
import com.wangmeng.book.utils.ConstValueUtils;
import com.wangmeng.book.utils.SpringContextManager;
import com.wangmeng.book.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.log4j.Logger;
import org.hibernate.engine.jdbc.BlobProxy;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * User: 王猛
 * Date: 13-9-10
 */
public class BookFromTx implements IBookUpdater {
    private static final Logger logger = Logger.getLogger(BookFromTx.class);
    public static int PAGE_SIZE = 100;
    private static String URL_WOMAN = "http://sight.qq.com/book/search?uin=0&g_tk=5381&callback=_Callback&cat1=-1&cat2=-1&cat3=-1&copyright=2&sex=2&words=-1&finish=-1&feetype=0&sortkey=1&page=#{page_num}&pagesize=#{page_size}&dm=0";
    private static String URL_MAN = "http://sight.qq.com/book/search?uin=0&g_tk=5381&callback=_Callback&cat1=-1&cat2=-1&cat3=-1&copyright=2&sex=1&words=-1&finish=-1&feetype=0&sortkey=1&page=#{page_num}&pagesize=#{page_size}&dm=0";
    private static String URL_BOOK = "http://sight.qq.com/book/search?uin=0&g_tk=5381&callback=_Callback&cat1=-1&cat2=-1&cat3=-1&copyright=2&sex=0&words=-1&finish=-1&feetype=0&sortkey=1&page=#{page_num}&pagesize=#{page_size}&dm=0";
    private static String URL_CHAPTER = "http://sight.qq.com/book/chapterpage?callback=_Callback&pagesize=#{page_size}&pageno=#{page_num}&bid=#{bid}";
    private static String URL_PAGE = "http://sight.qq.com/auth/read?callback=_Callback&bid=#{bid}&cid=#{cid}";
    private static String STATUS_URL = "http://sight.qq.com/book/intro?callback=_Callback&bid=#{bid}";

    public void updateList() {
        HttpClient hc = new DefaultHttpClient();
        hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
        hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
        CookieStore cs = new BasicCookieStore();
        HttpContext context = new BasicHttpContext();
        context.setAttribute(ClientContext.COOKIE_STORE, cs);
        HttpGet hg = new HttpGet(URL_WOMAN.replace("#{page_num}", "1").replace("#{page_size}", PAGE_SIZE + ""));
        try {
            HttpResponse hr = hc.execute(hg, context);
            if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                hr.getEntity().writeTo(bout);
                String content = new String(bout.toByteArray(), "UTF-8").trim();
                bout.close();
                if (content.length() > 12) {
                    JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
                    if (json != null) {
                        String retCode = json.getString("ret");
                        if (retCode.equals("0")) {
                            String totalStr = json.getString("total");
                            JSONArray bookList = json.getJSONArray("BookList");
                            dealList(bookList);
                            if (!StringUtils.isTrimEmpty(totalStr)) {
                                int total = Integer.valueOf(totalStr);
                                int cPage = 2;
                                for (; (cPage - 1) * PAGE_SIZE < total; cPage++) {
                                    try {
                                        hg = new HttpGet(URL_WOMAN.replace("#{page_num}", String.valueOf(cPage)).replace("#{page_size}", PAGE_SIZE + ""));
                                        hr = hc.execute(hg, context);
                                        if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                            bout = new ByteArrayOutputStream();
                                            hr.getEntity().writeTo(bout);
                                            content = new String(bout.toByteArray(), "UTF-8").trim();
                                            if (content.length() > 12) {
                                                json = new JSONObject(content.substring(10, content.length() - 1));
                                                if (json != null) {
                                                    retCode = json.getString("ret");
                                                    if (retCode.equals("0")) {
                                                        bookList = json.getJSONArray("BookList");
                                                        dealList(bookList);
                                                    }
                                                }
                                            }
                                        }
                                    } catch (Exception ex) {
                                        logger.error("TX更新第" + cPage + "页异常", ex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("更新TX图书发生错误", e);
        }
        hg = new HttpGet(URL_MAN.replace("#{page_num}", "1").replace("#{page_size}", PAGE_SIZE + ""));
        try {
            HttpResponse hr = hc.execute(hg, context);
            if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                hr.getEntity().writeTo(bout);
                String content = new String(bout.toByteArray(), "UTF-8").trim();
                bout.close();
                if (content.length() > 12) {
                    JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
                    if (json != null) {
                        String retCode = json.getString("ret");
                        if (retCode.equals("0")) {
                            String totalStr = json.getString("total");
                            JSONArray bookList = json.getJSONArray("BookList");
                            dealList(bookList);
                            if (!StringUtils.isTrimEmpty(totalStr)) {
                                int total = Integer.valueOf(totalStr);
                                int cPage = 2;
                                for (; (cPage - 1) * PAGE_SIZE < total; cPage++) {
                                    try {
                                        hg = new HttpGet(URL_WOMAN.replace("#{page_num}", String.valueOf(cPage)).replace("#{page_size}", PAGE_SIZE + ""));
                                        hr = hc.execute(hg, context);
                                        if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                            bout = new ByteArrayOutputStream();
                                            hr.getEntity().writeTo(bout);
                                            content = new String(bout.toByteArray(), "UTF-8").trim();
                                            if (content.length() > 12) {
                                                json = new JSONObject(content.substring(10, content.length() - 1));
                                                if (json != null) {
                                                    retCode = json.getString("ret");
                                                    if (retCode.equals("0")) {
                                                        bookList = json.getJSONArray("BookList");
                                                        dealList(bookList);
                                                    }
                                                }
                                            }
                                        }
                                    } catch (Exception ex) {
                                        logger.error("TX更新第" + cPage + "页异常", ex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("更新TX图书发生错误", e);
        }
        hg = new HttpGet(URL_BOOK.replace("#{page_num}", "1").replace("#{page_size}", PAGE_SIZE + ""));
        try {
            HttpResponse hr = hc.execute(hg, context);
            if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                hr.getEntity().writeTo(bout);
                String content = new String(bout.toByteArray(), "UTF-8").trim();
                bout.close();
                if (content.length() > 12) {
                    JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
                    if (json != null) {
                        String retCode = json.getString("ret");
                        if (retCode.equals("0")) {
                            String totalStr = json.getString("total");
                            JSONArray bookList = json.getJSONArray("BookList");
                            dealList(bookList);
                            if (!StringUtils.isTrimEmpty(totalStr)) {
                                int total = Integer.valueOf(totalStr);
                                int cPage = 2;
                                for (; (cPage - 1) * PAGE_SIZE < total; cPage++) {
                                    try {
                                        hg = new HttpGet(URL_WOMAN.replace("#{page_num}", String.valueOf(cPage)).replace("#{page_size}", PAGE_SIZE + ""));
                                        hr = hc.execute(hg, context);
                                        if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                            bout = new ByteArrayOutputStream();
                                            hr.getEntity().writeTo(bout);
                                            content = new String(bout.toByteArray(), "UTF-8").trim();
                                            if (content.length() > 12) {
                                                json = new JSONObject(content.substring(10, content.length() - 1));
                                                if (json != null) {
                                                    retCode = json.getString("ret");
                                                    if (retCode.equals("0")) {
                                                        bookList = json.getJSONArray("BookList");
                                                        dealList(bookList);
                                                    }
                                                }
                                            }
                                        }
                                    } catch (Exception ex) {
                                        logger.error("TX更新第" + cPage + "页异常", ex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("更新TX图书发生错误", e);
        }
    }

    private void dealList(JSONArray bookList) {
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            String checkSql = " and name = ? and author = ? ";
            String checkTagSql = "select oid from book_document_tag where tag_name = ? ";
            for (int i = 0; i < bookList.length(); i++) {
                JSONObject item = bookList.getJSONObject(i);
                String cateInfo = item.getString("categoryInfo");//"500:小说:小说,558:古代言情:古代言情,562:穿越奇情:穿越奇情"
                String tagInfo = item.getString("tag");//"1122:腹黑,1134:种田文,1143:复仇,1209:宅斗,1216:女强"
                String publisher = item.getString("publisher");//空
                String idStr = item.getString("id");//"224277"
                String authorName = item.getString("author").trim();//"要吃肉的羊"
                String iconImg = item.getString("cover");//"http:\/\/wfqqreader.3g.qq.com\/cover\/277\/224277\/b_224277.jpg"
                String title = item.getString("title").trim();//"一品庶女：贤妻惹邪夫"
                String free = item.getString("free").trim();//是否免费
                JSONObject countinfo = item.getJSONObject("countinfo");
                Integer viewCount = 0;
                if (countinfo != null) {
                    viewCount = countinfo.getInt("Read");
                }
                List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{title, authorName});
                if (bdList.size() <= 0) {
                    String wordNumStr = item.getString("totalWords");//"214541"
                    String finishedFlag = item.getString("finished");//"0"
                    String bookBrief = item.getString("intro");//"前世，安晓晴被小三推进湖中淹死，穿越重生，她成了不被承认的私生女。嫡母狠毒，庶姐霸道，
                    // 父亲自私自利。哼，不发威，你们拿我当病猫？斗嫡母，夺回小姐身份；打庶姐，报夺命之仇；治恶奴，为生母出气；
                    // 使手段，立威夺权！他贵为皇室贵胄，却爱上奴生的她，装疯卖傻得她怜惜，想方设法娶她回家，谁欺负她，他就恶整，
                    // 明明宠她无下限，却要天天气得她跳脚，抛却万里江山，只为赢她芳心一片。\r\n"
                    BookDoc bd = new BookDoc();
                    bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                    bd.setName(title);
                    bd.setViewCount(viewCount);
                    bd.setAuthor(authorName);
                    bd.setBookBrief(bookBrief);
                    bd.setPublisher(publisher);
                    bd.setSourceType(ConstValueUtils.SOURCE_TYPE_TX);
                    bd.setSourceId(idStr);
                    bd.setFree(StringUtils.isEmpty(free) ? 0 : (Integer.valueOf(free) == 1 ? 1 : 0));
                    bd.setHref("http://book.qq.com/intro.html?bid=" + idStr);
                    bd.setWordNum((wordNumStr != null && wordNumStr.trim().length() > 0) ? Integer.valueOf(wordNumStr.trim()) : null);
                    bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                    bd.setStatus(StringUtils.isTrimEmpty(finishedFlag) ? "" : (finishedFlag.equals("1") ? ConstValueUtils.STATUS_FINISHED : ConstValueUtils.STATUS_CONTINUE));
                    if (!StringUtils.isTrimEmpty(cateInfo)) {
                        bd.setCateName(cateInfo);
//                        String[] cateList = cateInfo.split(",");
//                        if (cateInfo.length() > 0) {
//                            String[] cateNames = cateList[0].split("\\:");
//                            if (cateNames.length >= 3) {
//                                bd.setPartition(cateNames[2]);
//                            }
//                        }
                    }
                    //处理图标信息
                    bd.setHasIcon(false);
                    if (!StringUtils.isTrimEmpty(iconImg)) {
                        HttpGet imgGet = new HttpGet(iconImg);
                        HttpClient hc = new DefaultHttpClient();
                        hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
                        hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
                        HttpResponse imgResp = hc.execute(imgGet);
                        if (imgResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                            ByteArrayOutputStream imgBuf = new ByteArrayOutputStream();
                            imgResp.getEntity().writeTo(imgBuf);
                            if (imgBuf.size() > 0) {
                                bd.setHasIcon(true);
                                bd.setIcon(BlobProxy.generateProxy(imgBuf.toByteArray()));
                            }
                            imgBuf.close();
                        }
                        hc.getConnectionManager().shutdown();
                    }
                    bd.setUpdateTime(new Date());
                    daoSupport.saveEntity(bd);
                    //处理TAG
                    if (!StringUtils.isTrimEmpty(tagInfo)) {
                        String[] tagList = tagInfo.split(",");
                        for (String tag : tagList) {
                            if (!StringUtils.isTrimEmpty(tag)) {
                                String[] tagValues = tag.split("\\:");
                                if (tagValues.length == 2) {
                                    int tagId = 0;
                                    String tv = tagValues[1].trim();
                                    List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{tv});
                                    if (tagDbList.size() > 0) {
                                        tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                                    } else {
                                        BookDocTag bt = new BookDocTag();
                                        bt.setName(tv);
                                        daoSupport.saveEntity(bt);
                                        tagId = bt.getId();
                                    }
                                    BookDocTagRelation tr = new BookDocTagRelation();
                                    tr.setBookId(bd.getId());
                                    tr.setTagId(tagId);
                                    daoSupport.saveEntity(tr);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("更新TX异常", ex);
        }
    }

    public int loadContent(BookDoc bd) {
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            List<BookDocSection> sectionList = daoSupport.listEntityByCondition(BookDocSection.class, " and bookId = ? order by order asc", new Object[]{bd.getId()});
            String maxPartSql = "select IFNULL(max(part_order),0) mo from book_document_part where section_id in ( select oid from book_document_section where book_id = ? )";
            int maxPartNum = jt.queryForInt(maxPartSql, new Object[]{bd.getId()});
            HttpClient hc = new DefaultHttpClient();
            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
            int pageSize = 100;
            int bPage = maxPartNum > 0 ? Double.valueOf(Math.ceil((maxPartNum + 1.0) / pageSize)).intValue() : 1;
            HttpGet hg = new HttpGet(URL_CHAPTER.replace("#{page_size}", String.valueOf(pageSize)).replace("#{page_num}", String.valueOf(bPage)).replace("#{bid}", bd.getSourceId()));
            HttpResponse resp = hc.execute(hg);
            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                resp.getEntity().writeTo(bout);
                String content = new String(bout.toByteArray(), "UTF-8").trim();
                bout.close();
                JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
                if (json != null) {
                    int ret = json.getInt("ret");
                    if (ret == ConstValueUtils.CODE_SUCCESS) {
                        int totalWords = json.getJSONObject("info").getInt("totalWords");
                        if (bd.getWordNum() == null) {
                            bd.setWordNum(totalWords);
                            daoSupport.saveEntity(bd);
                        }
                        JSONArray volumes = json.has("volumes") ? json.getJSONArray("volumes") : new JSONArray();
                        if (volumes.length() <= 0) {
                            if (sectionList.size() == 0) {
                                BookDocSection bds = new BookDocSection();
                                bds.setName("目录");
                                bds.setOrder(1);
                                bds.setBookId(bd.getId());
                                daoSupport.saveEntity(bds);
                                bds.setFrom(0);
                                bds.setTo(Integer.MAX_VALUE);
                                sectionList.add(bds);
                            }
                        } else {
                            for (int i = 0; i < volumes.length(); i++) {
                                JSONObject sItem = volumes.getJSONObject(i);
                                String sTitle = sItem.getString("title").trim();
                                int seq = sItem.getInt("seq");
                                int from = sItem.getInt("from");
                                int to = sItem.getInt("to");
                                if (seq <= sectionList.size()) {
                                    BookDocSection bds = sectionList.get(seq - 1);
                                    if (!bds.getName().equals(sTitle)) {
                                        bds.setName(sTitle);
                                        daoSupport.saveEntity(bds);
                                    }
                                    bds.setFrom(from);
                                    bds.setTo(to);
                                } else {
                                    BookDocSection bds = new BookDocSection();
                                    bds.setName(sTitle);
                                    bds.setOrder(sItem.getInt("seq"));
                                    bds.setBookId(bd.getId());
                                    daoSupport.saveEntity(bds);
                                    bds.setFrom(from);
                                    bds.setTo(to);
                                    sectionList.add(bds);
                                }
                            }
                        }
                        //处理页
                        JSONObject pageData = json.getJSONObject("page");
                        Boolean hasNext = pageData.getBoolean("hasNext");
                        //先处理本次记录
                        JSONArray records = pageData.getJSONArray("record");
                        for (int r = 0; r < records.length(); r++) {
                            JSONObject record = records.getJSONObject(r);
                            int seq = record.getInt("seq");
                            if (seq > maxPartNum) {
                                BookDocSection currentBds = null;
                                for (BookDocSection bs : sectionList) {
                                    if (bs.getFrom() != null && seq >= bs.getFrom() && seq <= bs.getTo()) {
                                        currentBds = bs;
                                        break;
                                    }
                                }
                                if (currentBds != null) {
                                    String pageTitle = record.getString("title");
                                    String text = getPageText(bd.getSourceId(), String.valueOf(seq));
                                    BookDocPart bdp = new BookDocPart();
                                    bdp.setOrder(seq);
                                    bdp.setTitle(pageTitle);
                                    bdp.setContent(text);
                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                    bdp.setSectionId(currentBds.getId());
                                    bdp.setUpdateTime(new Date());
                                    daoSupport.saveEntity(bdp);
                                }
                            }
                        }
                        //处理后续记录
                        while (hasNext) {
                            hg = new HttpGet(URL_CHAPTER.replace("#{page_size}", String.valueOf(pageSize)).replace("#{page_num}", String.valueOf(++bPage)).replace("#{bid}", String.valueOf(bd.getSourceId())));
                            resp = hc.execute(hg);
                            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                bout = new ByteArrayOutputStream();
                                resp.getEntity().writeTo(bout);
                                content = new String(bout.toByteArray(), "UTF-8").trim();
                                bout.close();
                                json = new JSONObject(content.substring(10, content.length() - 1));
                                if (json != null) {
                                    ret = json.getInt("ret");
                                    if (ret == ConstValueUtils.CODE_SUCCESS) {
                                        volumes = json.has("volumes") ? json.getJSONArray("volumes") : new JSONArray();
                                        if (volumes.length() <= 0) {
                                            if (sectionList.size() == 0) {
                                                BookDocSection bds = new BookDocSection();
                                                bds.setName("目录");
                                                bds.setOrder(1);
                                                bds.setBookId(bd.getId());
                                                daoSupport.saveEntity(bds);
                                                bds.setFrom(0);
                                                bds.setTo(Integer.MAX_VALUE);
                                                sectionList.add(bds);
                                            }
                                        } else {
                                            for (int i = 0; i < volumes.length(); i++) {
                                                JSONObject sItem = volumes.getJSONObject(i);
                                                String sTitle = sItem.getString("title").trim();
                                                int seq = sItem.getInt("seq");
                                                int from = sItem.getInt("from");
                                                int to = sItem.getInt("to");
                                                if (seq <= sectionList.size()) {
                                                    BookDocSection bds = sectionList.get(seq - 1);
                                                    if (!bds.getName().equals(sTitle)) {
                                                        bds.setName(sTitle);
                                                        daoSupport.saveEntity(bds);
                                                    }
                                                    bds.setFrom(from);
                                                    bds.setTo(to);
                                                } else {
                                                    BookDocSection bds = new BookDocSection();
                                                    bds.setName(sTitle);
                                                    bds.setOrder(sItem.getInt("seq"));
                                                    bds.setBookId(bd.getId());
                                                    daoSupport.saveEntity(bds);
                                                    bds.setFrom(from);
                                                    bds.setTo(to);
                                                    sectionList.add(bds);
                                                }
                                            }
                                        }
                                        pageData = json.getJSONObject("page");
                                        hasNext = pageData.getBoolean("hasNext");
                                        records = pageData.getJSONArray("record");
                                        for (int r = 0; r < records.length(); r++) {
                                            JSONObject record = records.getJSONObject(r);
                                            int seq = record.getInt("seq");
                                            if (seq > maxPartNum) {
                                                BookDocSection currentBds = null;
                                                for (BookDocSection bs : sectionList) {
                                                    if (bs.getFrom() != null && seq >= bs.getFrom() && seq <= bs.getTo()) {
                                                        currentBds = bs;
                                                        break;
                                                    }
                                                }
                                                if (currentBds != null) {
                                                    String pageTitle = record.getString("title");
                                                    String text = getPageText(bd.getSourceId(), String.valueOf(seq));
                                                    BookDocPart bdp = new BookDocPart();
                                                    bdp.setOrder(seq);
                                                    bdp.setTitle(pageTitle);
                                                    bdp.setContent(text);
                                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                                    bdp.setSectionId(currentBds.getId());
                                                    bdp.setUpdateTime(new Date());
                                                    daoSupport.saveEntity(bdp);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("更新" + bd.getId() + "章节内容异常", ex);
            return ConstValueUtils.CODE_EXCEPTION;
        }
        return ConstValueUtils.CODE_SUCCESS;
    }

    public int updateContent(BookDoc bd) {
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            List<BookDocSection> sectionList = daoSupport.listEntityByCondition(BookDocSection.class, " and bookId = ? order by order asc", new Object[]{bd.getId()});
            String maxPartSql = "select IFNULL(max(part_order),0) mo from book_document_part where section_id in ( select oid from book_document_section where book_id = ? )";
            int maxPartNum = jt.queryForInt(maxPartSql, new Object[]{bd.getId()});
            HttpClient hc = new DefaultHttpClient();
            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
            int pageSize = 100;
            int currentMaxPartNum = maxPartNum;
            int bPage = maxPartNum > 0 ? Double.valueOf(Math.ceil((maxPartNum + 1.0) / pageSize)).intValue() : 1;
            HttpGet hg = new HttpGet(URL_CHAPTER.replace("#{page_size}", String.valueOf(pageSize)).replace("#{page_num}", String.valueOf(bPage)).replace("#{bid}", bd.getSourceId()));
            HttpResponse resp = hc.execute(hg);
            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                resp.getEntity().writeTo(bout);
                String content = new String(bout.toByteArray(), "UTF-8").trim();
                bout.close();
                JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
                if (json != null) {
                    int ret = json.getInt("ret");
                    if (ret == ConstValueUtils.CODE_SUCCESS) {
                        int totalWords = json.getJSONObject("info").getInt("totalWords");
                        if (bd.getWordNum() == null || totalWords > bd.getWordNum()) {
                            bd.setWordNum(totalWords);
                            daoSupport.saveEntity(bd);
                        }
                        JSONArray volumes = json.has("volumes") ? json.getJSONArray("volumes") : new JSONArray();
                        if (volumes.length() <= 0) {
                            if (sectionList.size() == 0) {
                                BookDocSection bds = new BookDocSection();
                                bds.setName("目录");
                                bds.setOrder(1);
                                bds.setBookId(bd.getId());
                                daoSupport.saveEntity(bds);
                                bds.setFrom(0);
                                bds.setTo(Integer.MAX_VALUE);
                                sectionList.add(bds);
                            }
                        } else {
                            for (int i = 0; i < volumes.length(); i++) {
                                JSONObject sItem = volumes.getJSONObject(i);
                                String sTitle = sItem.getString("title").trim();
                                int seq = sItem.getInt("seq");
                                int from = sItem.getInt("from");
                                int to = sItem.getInt("to");
                                if (seq <= sectionList.size()) {
                                    BookDocSection bds = sectionList.get(seq - 1);
                                    if (!bds.getName().equals(sTitle)) {
                                        bds.setName(sTitle);
                                        daoSupport.saveEntity(bds);
                                    }
                                    bds.setFrom(from);
                                    bds.setTo(to);
                                } else {
                                    BookDocSection bds = new BookDocSection();
                                    bds.setName(sTitle);
                                    bds.setOrder(sItem.getInt("seq"));
                                    bds.setBookId(bd.getId());
                                    daoSupport.saveEntity(bds);
                                    bds.setFrom(from);
                                    bds.setTo(to);
                                    sectionList.add(bds);
                                }
                            }
                        }
                        //处理页
                        JSONObject pageData = json.getJSONObject("page");
                        Boolean hasNext = pageData.getBoolean("hasNext");
                        //先处理本次记录
                        JSONArray records = pageData.getJSONArray("record");
                        for (int r = 0; r < records.length(); r++) {
                            JSONObject record = records.getJSONObject(r);
                            int seq = record.getInt("seq");
                            if (seq > maxPartNum) {
                                BookDocSection currentBds = null;
                                for (BookDocSection bs : sectionList) {
                                    if (bs.getFrom() != null && seq >= bs.getFrom() && seq <= bs.getTo()) {
                                        currentBds = bs;
                                        break;
                                    }
                                }
                                if (currentBds != null) {
                                    String pageTitle = record.getString("title");
                                    String text = getPageText(bd.getSourceId(), String.valueOf(seq));
                                    BookDocPart bdp = new BookDocPart();
                                    bdp.setOrder(seq);
                                    bdp.setTitle(pageTitle);
                                    bdp.setContent(text);
                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                    bdp.setSectionId(currentBds.getId());
                                    bdp.setUpdateTime(new Date());
                                    daoSupport.saveEntity(bdp);
                                    currentMaxPartNum = seq;
                                }
                            }
                        }
                        //处理后续记录
                        while (hasNext) {
                            hg = new HttpGet(URL_CHAPTER.replace("#{page_size}", String.valueOf(pageSize)).replace("#{page_num}", String.valueOf(++bPage)).replace("#{bid}", String.valueOf(bd.getSourceId())));
                            resp = hc.execute(hg);
                            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                bout = new ByteArrayOutputStream();
                                resp.getEntity().writeTo(bout);
                                content = new String(bout.toByteArray(), "UTF-8").trim();
                                bout.close();
                                json = new JSONObject(content.substring(10, content.length() - 1));
                                if (json != null) {
                                    ret = json.getInt("ret");
                                    if (ret == ConstValueUtils.CODE_SUCCESS) {
                                        volumes = json.has("volumes") ? json.getJSONArray("volumes") : new JSONArray();
                                        if (volumes.length() > 0) {
                                            for (int i = 0; i < volumes.length(); i++) {
                                                JSONObject sItem = volumes.getJSONObject(i);
                                                String sTitle = sItem.getString("title").trim();
                                                int seq = sItem.getInt("seq");
                                                int from = sItem.getInt("from");
                                                int to = sItem.getInt("to");
                                                if (seq <= sectionList.size()) {
                                                    BookDocSection bds = sectionList.get(seq - 1);
                                                    if (!bds.getName().equals(sTitle)) {
                                                        bds.setName(sTitle);
                                                        daoSupport.saveEntity(bds);
                                                    }
                                                    bds.setFrom(from);
                                                    bds.setTo(to);
                                                } else {
                                                    BookDocSection bds = new BookDocSection();
                                                    bds.setName(sTitle);
                                                    bds.setOrder(sItem.getInt("seq"));
                                                    bds.setBookId(bd.getId());
                                                    daoSupport.saveEntity(bds);
                                                    bds.setFrom(from);
                                                    bds.setTo(to);
                                                    sectionList.add(bds);
                                                }
                                            }
                                        }
                                        pageData = json.getJSONObject("page");
                                        hasNext = pageData.getBoolean("hasNext");
                                        records = pageData.getJSONArray("record");
                                        for (int r = 0; r < records.length(); r++) {
                                            JSONObject record = records.getJSONObject(r);
                                            int seq = record.getInt("seq");
                                            if (seq > maxPartNum) {
                                                BookDocSection currentBds = null;
                                                for (BookDocSection bs : sectionList) {
                                                    if (bs.getFrom() != null && seq >= bs.getFrom() && seq <= bs.getTo()) {
                                                        currentBds = bs;
                                                        break;
                                                    }
                                                }
                                                if (currentBds != null) {
                                                    String pageTitle = record.getString("title");
                                                    String text = getPageText(bd.getSourceId(), String.valueOf(seq));
                                                    BookDocPart bdp = new BookDocPart();
                                                    bdp.setOrder(seq);
                                                    bdp.setTitle(pageTitle);
                                                    bdp.setContent(text);
                                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                                    bdp.setSectionId(currentBds.getId());
                                                    bdp.setUpdateTime(new Date());
                                                    daoSupport.saveEntity(bdp);
                                                    currentMaxPartNum = seq;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //检查是否已经完结
            String url = STATUS_URL.replace("#{bid}", bd.getSourceId());
            hg = new HttpGet(url);
            resp = hc.execute(hg);
            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                resp.getEntity().writeTo(bout);
                String content = new String(bout.toByteArray(), "UTF-8").trim();
                bout.close();
                JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
                if (json != null) {
                    int ret = json.getInt("ret");
                    if (ret == ConstValueUtils.CODE_SUCCESS) {
                        if (json.has("finished") && json.getString("finished").equals("1")) {
                            bd.setStatus(ConstValueUtils.STATUS_FINISHED);
                            daoSupport.saveEntity(bd);
                        }
                    }
                }
            }
            int count = currentMaxPartNum - maxPartNum;
            if (count > 0) {
                bd.setApproveStatus(ConstValueUtils.APPROVE_HAS_UPDATE);
                daoSupport.saveEntity(bd);
            }
            return count;
        } catch (Exception ex) {
            logger.error("更新" + bd.getId() + "章节内容异常", ex);
            return 0;
        }
    }

    private String getPageText(String bid, String cid) {
        String text = "";
        HttpClient hc = new DefaultHttpClient();
        hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
        hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
        HttpGet httpGet = new HttpGet(URL_PAGE.replace("#{bid}", bid).replace("#{cid}", cid));
        try {
            HttpResponse resp = hc.execute(httpGet);
            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                resp.getEntity().writeTo(bout);
                String content = new String(bout.toByteArray(), "UTF-8").trim();
                bout.close();
                JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
                if (json != null) {
                    int ret = json.getInt("ret");
                    if (ret == ConstValueUtils.CODE_SUCCESS) {
                        text = json.getString("content").trim();
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("采集信息异常:bid->" + bid + ",cid->" + cid, ex);
        }
        return text;
    }

//    public void update() {
//        HttpClient hc = new DefaultHttpClient();
//        hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
//        hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
//        CookieStore cs = new BasicCookieStore();
//        HttpContext context = new BasicHttpContext();
//        context.setAttribute(ClientContext.COOKIE_STORE, cs);
//        HttpGet hg = new HttpGet(URL_WOMAN.replace("#{page_num}", "1").replace("#{page_size}", PAGE_SIZE + ""));
//        try {
//            HttpResponse hr = hc.execute(hg, context);
//            if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                ByteArrayOutputStream bout = new ByteArrayOutputStream();
//                hr.getEntity().writeTo(bout);
//                String content = bout.toString().trim();
//                bout.close();
//                if (content.length() > 12) {
//                    JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
//                    if (json != null) {
//                        String retCode = json.getString("ret");
//                        if (retCode.equals("0")) {
//                            String totalStr = json.getString("total");
//                            JSONArray bookList = json.getJSONArray("BookList");
//                            updateBook(bookList);
//                            if (!StringUtils.isTrimEmpty(totalStr)) {
//                                int total = Integer.valueOf(totalStr);
//                                int cPage = 2;
//                                for (; (cPage - 1) * PAGE_SIZE < total; cPage++) {
//                                    try {
//                                        hg = new HttpGet(URL_WOMAN.replace("#{page_num}", String.valueOf(cPage)).replace("#{page_size}", PAGE_SIZE + ""));
//                                        hr = hc.execute(hg, context);
//                                        if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                                            bout = new ByteArrayOutputStream();
//                                            hr.getEntity().writeTo(bout);
//                                            content = bout.toString().trim();
//                                            if (content.length() > 12) {
//                                                json = new JSONObject(content.substring(10, content.length() - 1));
//                                                if (json != null) {
//                                                    retCode = json.getString("ret");
//                                                    if (retCode.equals("0")) {
//                                                        bookList = json.getJSONArray("BookList");
//                                                        updateBook(bookList);
//                                                    }
//                                                }
//                                            }
//                                        }
//                                    } catch (Exception ex) {
//                                        logger.error("TX更新第" + cPage + "页异常", ex);
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            logger.error("更新TX图书发生错误", e);
//        }
//        hg = new HttpGet(URL_MAN.replace("#{page_num}", "1").replace("#{page_size}", PAGE_SIZE + ""));
//        try {
//            HttpResponse hr = hc.execute(hg, context);
//            if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                ByteArrayOutputStream bout = new ByteArrayOutputStream();
//                hr.getEntity().writeTo(bout);
//                String content = bout.toString().trim();
//                bout.close();
//                if (content.length() > 12) {
//                    JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
//                    if (json != null) {
//                        String retCode = json.getString("ret");
//                        if (retCode.equals("0")) {
//                            String totalStr = json.getString("total");
//                            JSONArray bookList = json.getJSONArray("BookList");
//                            updateBook(bookList);
//                            if (!StringUtils.isTrimEmpty(totalStr)) {
//                                int total = Integer.valueOf(totalStr);
//                                int cPage = 2;
//                                for (; (cPage - 1) * PAGE_SIZE < total; cPage++) {
//                                    try {
//                                        hg = new HttpGet(URL_WOMAN.replace("#{page_num}", String.valueOf(cPage)).replace("#{page_size}", PAGE_SIZE + ""));
//                                        hr = hc.execute(hg, context);
//                                        if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                                            bout = new ByteArrayOutputStream();
//                                            hr.getEntity().writeTo(bout);
//                                            content = bout.toString().trim();
//                                            if (content.length() > 12) {
//                                                json = new JSONObject(content.substring(10, content.length() - 1));
//                                                if (json != null) {
//                                                    retCode = json.getString("ret");
//                                                    if (retCode.equals("0")) {
//                                                        bookList = json.getJSONArray("BookList");
//                                                        updateBook(bookList);
//                                                    }
//                                                }
//                                            }
//                                        }
//                                    } catch (Exception ex) {
//                                        logger.error("TX更新第" + cPage + "页异常", ex);
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            logger.error("更新TX图书发生错误", e);
//        }
//        hg = new HttpGet(URL_BOOK.replace("#{page_num}", "1").replace("#{page_size}", PAGE_SIZE + ""));
//        try {
//            HttpResponse hr = hc.execute(hg, context);
//            if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                ByteArrayOutputStream bout = new ByteArrayOutputStream();
//                hr.getEntity().writeTo(bout);
//                String content = bout.toString().trim();
//                bout.close();
//                if (content.length() > 12) {
//                    JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
//                    if (json != null) {
//                        String retCode = json.getString("ret");
//                        if (retCode.equals("0")) {
//                            String totalStr = json.getString("total");
//                            JSONArray bookList = json.getJSONArray("BookList");
//                            updateBook(bookList);
//                            if (!StringUtils.isTrimEmpty(totalStr)) {
//                                int total = Integer.valueOf(totalStr);
//                                int cPage = 2;
//                                for (; (cPage - 1) * PAGE_SIZE < total; cPage++) {
//                                    try {
//                                        hg = new HttpGet(URL_WOMAN.replace("#{page_num}", String.valueOf(cPage)).replace("#{page_size}", PAGE_SIZE + ""));
//                                        hr = hc.execute(hg, context);
//                                        if (hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                                            bout = new ByteArrayOutputStream();
//                                            hr.getEntity().writeTo(bout);
//                                            content = bout.toString().trim();
//                                            if (content.length() > 12) {
//                                                json = new JSONObject(content.substring(10, content.length() - 1));
//                                                if (json != null) {
//                                                    retCode = json.getString("ret");
//                                                    if (retCode.equals("0")) {
//                                                        bookList = json.getJSONArray("BookList");
//                                                        updateBook(bookList);
//                                                    }
//                                                }
//                                            }
//                                        }
//                                    } catch (Exception ex) {
//                                        logger.error("TX更新第" + cPage + "页异常", ex);
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            logger.error("更新TX图书发生错误", e);
//        }
//    }
//
//    private void updateBook(JSONArray bookList) {
//        try {
//            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
//            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
//            String checkSql = " and name = ? and author = ? ";
//            String checkTagSql = "select oid from book_document_tag where tag_name = ? ";
//            for (int i = 0; i < bookList.length(); i++) {
//                JSONObject item = bookList.getJSONObject(i);
//                String cateInfo = item.getString("categoryInfo");//"500:小说:小说,558:古代言情:古代言情,562:穿越奇情:穿越奇情"
//                String tagInfo = item.getString("tag");//"1122:腹黑,1134:种田文,1143:复仇,1209:宅斗,1216:女强"
//                String publisher = item.getString("publisher");//空
//                String idStr = item.getString("id");//"224277"
//                String authorName = item.getString("author").trim();//"要吃肉的羊"
//                String iconImg = item.getString("cover");//"http:\/\/wfqqreader.3g.qq.com\/cover\/277\/224277\/b_224277.jpg"
//                String title = item.getString("title").trim();//"一品庶女：贤妻惹邪夫"
//                List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{title, authorName});
//                if (bdList.size() <= 0) {
//                    String wordNumStr = item.getString("totalWords");//"214541"
//                    String finishedFlag = item.getString("finished");//"0"
//                    String bookBrief = item.getString("intro");//"前世，安晓晴被小三推进湖中淹死，穿越重生，她成了不被承认的私生女。嫡母狠毒，庶姐霸道，
//                    // 父亲自私自利。哼，不发威，你们拿我当病猫？斗嫡母，夺回小姐身份；打庶姐，报夺命之仇；治恶奴，为生母出气；
//                    // 使手段，立威夺权！他贵为皇室贵胄，却爱上奴生的她，装疯卖傻得她怜惜，想方设法娶她回家，谁欺负她，他就恶整，
//                    // 明明宠她无下限，却要天天气得她跳脚，抛却万里江山，只为赢她芳心一片。\r\n"
//                    BookDoc bd = new BookDoc();
//                    bd.setName(title);
//                    bd.setAuthor(authorName);
//                    bd.setBookBrief(bookBrief);
//                    bd.setPublisher(publisher);
//                    bd.setSourceType(ConstValueUtils.SOURCE_TYPE_TX);
//                    bd.setSourceId(idStr);
//                    bd.setWordNum((wordNumStr != null && wordNumStr.trim().length() > 0) ? Integer.valueOf(idStr.trim()) : null);
//                    bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
//                    bd.setStatus(StringUtils.isTrimEmpty(finishedFlag) ? "" : (finishedFlag.equals("1") ? ConstValueUtils.STATUS_FINISHED : ConstValueUtils.STATUS_CONTINUE));
//                    if (!StringUtils.isTrimEmpty(cateInfo)) {
//                        String[] cateList = cateInfo.split(",");
//                        if (cateInfo.length() > 0) {
//                            String[] cateNames = cateList[0].split("\\:");
//                            if (cateNames.length >= 3) {
//                                bd.setPartition(cateNames[2]);
//                            }
//                        }
//                    }
//                    //处理图标信息
//                    bd.setHasIcon(false);
//                    if (!StringUtils.isTrimEmpty(iconImg)) {
//                        HttpGet imgGet = new HttpGet(iconImg);
//                        HttpClient hc = new DefaultHttpClient();
//                        hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
//                        hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
//                        HttpResponse imgResp = hc.execute(imgGet);
//                        if (imgResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                            ByteArrayOutputStream imgBuf = new ByteArrayOutputStream();
//                            imgResp.getEntity().writeTo(imgBuf);
//                            if (imgBuf.size() > 0) {
//                                bd.setHasIcon(true);
//                                bd.setIcon(BlobProxy.generateProxy(imgBuf.toByteArray()));
//                            }
//                            imgBuf.close();
//                        }
//                        hc.getConnectionManager().shutdown();
//                    }
//                    bd.setUpdateTime(new Date());
//                    daoSupport.saveEntity(bd);
//                    //处理TAG
//                    if (!StringUtils.isTrimEmpty(tagInfo)) {
//                        String[] tagList = tagInfo.split(",");
//                        for (String tag : tagList) {
//                            if (!StringUtils.isTrimEmpty(tag)) {
//                                String[] tagValues = tag.split("\\:");
//                                if (tagValues.length == 2) {
//                                    int tagId = 0;
//                                    String tv = tagValues[1].trim();
//                                    List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{tv});
//                                    if (tagDbList.size() > 0) {
//                                        tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
//                                    } else {
//                                        BookDocTag bt = new BookDocTag();
//                                        bt.setName(tv);
//                                        daoSupport.saveEntity(bt);
//                                        tagId = bt.getId();
//                                    }
//                                    BookDocTagRelation tr = new BookDocTagRelation();
//                                    tr.setBookId(bd.getId());
//                                    tr.setTagId(tagId);
//                                    daoSupport.saveEntity(tr);
//                                }
//                            }
//                        }
//                    }
//                    //更新文章内容
//                    updateBookContent(bd);
//                } else {
//                    //不新建但是如果未完结，需要检查是否有更新
//                    BookDoc bd = bdList.get(0);
//                    if (bd.getStatus().equals(ConstValueUtils.STATUS_CONTINUE)) {
//                        updateBookContent(bd);
//                    }
//                }
//            }
//        } catch (Exception ex) {
//            logger.error("更新TX异常", ex);
//        }
//    }
//
//    private int updateBookContent(BookDoc bd) {
//        try {
//            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
//            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
//            List<BookDocSection> sectionList = daoSupport.listEntityByCondition(BookDocSection.class, " and bookId = ? order by order asc", new Object[]{bd.getId()});
//            String maxPartSql = "select IFNULL(max(part_order),0) mo from book_document_part where section_id in ( select oid from book_document_section where book_id = ? )";
//            int maxPartNum = jt.queryForInt(maxPartSql, new Object[]{bd.getId()});
//            HttpClient hc = new DefaultHttpClient();
//            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
//            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
//            int pageSize = 100;
//            int bPage = maxPartNum > 0 ? Double.valueOf(Math.ceil((maxPartNum + 1.0) / pageSize)).intValue() : 1;
//            HttpGet hg = new HttpGet(URL_CHAPTER.replace("#{page_size}", String.valueOf(pageSize)).replace("#{page_num}", String.valueOf(bPage)).replace("#{bid}", bd.getSourceId()));
//            HttpResponse resp = hc.execute(hg);
//            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                ByteArrayOutputStream bout = new ByteArrayOutputStream();
//                resp.getEntity().writeTo(bout);
//                String content = bout.toString().trim();
//                bout.close();
//                JSONObject json = new JSONObject(content.substring(10, content.length() - 1));
//                if (json != null) {
//                    int ret = json.getInt("ret");
//                    if (ret == ConstValueUtils.CODE_SUCCESS) {
//                        int totalWords = json.getInt("totalWords");
//                        if (bd.getWordNum() == null || totalWords > bd.getWordNum()) {
//                            bd.setWordNum(totalWords);
//                            daoSupport.saveEntity(bd);
//                        }
//                        JSONArray volumes = json.getJSONArray("volumes");
////                        int vNum = json.getJSONObject("info").getInt("hasVolumn");
//                        if (volumes.length() <= 0) {
//                            if (sectionList.size() == 0) {
//                                BookDocSection bds = new BookDocSection();
//                                bds.setName("目录");
//                                bds.setOrder(1);
//                                bds.setBookId(bd.getId());
//                                daoSupport.saveEntity(bds);
//                                bds.setFrom(0);
//                                bds.setTo(Integer.MAX_VALUE);
//                                sectionList.add(bds);
//                            }
//                        } else {
//                            for (int i = 0; i < volumes.length(); i++) {
//                                JSONObject sItem = volumes.getJSONObject(i);
//                                String sTitle = sItem.getString("title").trim();
//                                int seq = sItem.getInt("seq");
//                                int from = sItem.getInt("from");
//                                int to = sItem.getInt("to");
//                                if (seq <= sectionList.size()) {
//                                    BookDocSection bds = sectionList.get(seq - 1);
//                                    if (!bds.getName().equals(sTitle)) {
//                                        bds.setName(sTitle);
//                                        daoSupport.saveEntity(bds);
//                                    }
//                                    bds.setFrom(from);
//                                    bds.setTo(to);
//                                } else {
//                                    BookDocSection bds = new BookDocSection();
//                                    bds.setName(sTitle);
//                                    bds.setOrder(sItem.getInt("seq"));
//                                    bds.setBookId(bd.getId());
//                                    daoSupport.saveEntity(bds);
//                                    bds.setFrom(from);
//                                    bds.setTo(to);
//                                    sectionList.add(bds);
//                                }
//                            }
//                        }
//                        //处理页
//                        JSONObject pageData = json.getJSONObject("page");
//                        Boolean hasNext = pageData.getBoolean("hasNext");
//                        //先处理本次记录
//                        JSONArray records = pageData.getJSONArray("record");
//                        for (int r = 0; r < records.length(); r++) {
//                            JSONObject record = records.getJSONObject(r);
//                            int seq = record.getInt("seq");
//                            if (seq > maxPartNum) {
//                                BookDocSection currentBds = null;
//                                for (BookDocSection bs : sectionList) {
//                                    if (bs.getFrom() != null && seq >= bs.getFrom() && seq <= bs.getTo()) {
//                                        currentBds = bs;
//                                        break;
//                                    }
//                                }
//                                if (currentBds != null) {
//                                    String pageTitle = record.getString("title");
//                                    String text = getPageText(bd.getSourceId(), String.valueOf(seq));
//                                    BookDocPart bdp = new BookDocPart();
//                                    bdp.setOrder(seq);
//                                    bdp.setTitle(pageTitle);
//                                    bdp.setContent(text);
//                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
//                                    bdp.setSectionId(currentBds.getId());
//                                    bdp.setUpdateTime(new Date());
//                                    daoSupport.saveEntity(bdp);
//                                }
//                            }
//                        }
//                        //处理后续记录
//                        while (hasNext) {
//                            hg = new HttpGet(URL_CHAPTER.replace("#{page_size}", String.valueOf(pageSize)).replace("#{page_num}", String.valueOf(++bPage)).replace("#{bid}", String.valueOf(bd.getSourceId())));
//                            resp = hc.execute(hg);
//                            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                                bout = new ByteArrayOutputStream();
//                                resp.getEntity().writeTo(bout);
//                                content = bout.toString().trim();
//                                bout.close();
//                                json = new JSONObject(content.substring(10, content.length() - 1));
//                                if (json != null) {
//                                    ret = json.getInt("ret");
//                                    if (ret == ConstValueUtils.CODE_SUCCESS) {
//                                        volumes = json.getJSONArray("volumes");
//                                        if (volumes.length() > 0) {
//                                            for (int i = 0; i < volumes.length(); i++) {
//                                                JSONObject sItem = volumes.getJSONObject(i);
//                                                String sTitle = sItem.getString("title").trim();
//                                                int seq = sItem.getInt("seq");
//                                                int from = sItem.getInt("from");
//                                                int to = sItem.getInt("to");
//                                                if (seq <= sectionList.size()) {
//                                                    BookDocSection bds = sectionList.get(seq - 1);
//                                                    if (!bds.getName().equals(sTitle)) {
//                                                        bds.setName(sTitle);
//                                                        daoSupport.saveEntity(bds);
//                                                    }
//                                                    bds.setFrom(from);
//                                                    bds.setTo(to);
//                                                } else {
//                                                    BookDocSection bds = new BookDocSection();
//                                                    bds.setName(sTitle);
//                                                    bds.setOrder(sItem.getInt("seq"));
//                                                    bds.setBookId(bd.getId());
//                                                    daoSupport.saveEntity(bds);
//                                                    bds.setFrom(from);
//                                                    bds.setTo(to);
//                                                    sectionList.add(bds);
//                                                }
//                                            }
//                                        }
//                                        pageData = json.getJSONObject("page");
//                                        hasNext = pageData.getBoolean("hasNext");
//                                        records = pageData.getJSONArray("record");
//                                        for (int r = 0; r < records.length(); r++) {
//                                            JSONObject record = records.getJSONObject(r);
//                                            int seq = record.getInt("seq");
//                                            if (seq > maxPartNum) {
//                                                BookDocSection currentBds = null;
//                                                for (BookDocSection bs : sectionList) {
//                                                    if (bs.getFrom() != null && seq >= bs.getFrom() && seq <= bs.getTo()) {
//                                                        currentBds = bs;
//                                                        break;
//                                                    }
//                                                }
//                                                if (currentBds != null) {
//                                                    String pageTitle = record.getString("title");
//                                                    String text = getPageText(bd.getSourceId(), String.valueOf(seq));
//                                                    BookDocPart bdp = new BookDocPart();
//                                                    bdp.setOrder(seq);
//                                                    bdp.setTitle(pageTitle);
//                                                    bdp.setContent(text);
//                                                    bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
//                                                    bdp.setSectionId(currentBds.getId());
//                                                    bdp.setUpdateTime(new Date());
//                                                    daoSupport.saveEntity(bdp);
//                                                }
//                                            }
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        } catch (Exception ex) {
//            logger.error("更新" + bd.getId() + "章节内容异常", ex);
//            return ConstValueUtils.CODE_EXCEPTION;
//        }
//        return ConstValueUtils.CODE_SUCCESS;
//    }
}
